//
//  HZHomeTc.m
//  微博 OC
//
//  Created by 黑眼圈 on 16/4/18.
//  Copyright © 2016年 heiyan. All rights reserved.
//

#import "HZHomeTc.h"
#import "HZLoginView.h"
#import "HZOAuthVM.h"
#import "HZHomeVM.h"
#import "HZHomeModel.h"
#import "HZHomeCell.h"
#import "HZHomeCellVM.h"




@interface HZHomeTc ()

@property(nonatomic,strong)NSArray*   array;

@end

@implementation HZHomeTc


#pragma mark -
#pragma mark     重写数组的 set方法来赋值
-(void)setArray:(NSArray *)array{
    _array=array;
    
    
    //一有数据就刷新
    [self.tableView reloadData];

}



- (void)viewDidLoad {
    [super viewDidLoad];

    //如果没有登录才显示访客视图
    if (![[HZOAuthVM sharedOAuth] isslogined]) {
        
        
        HZLoginView* logView = (HZLoginView*)self.view;
        
        [logView setImg:nil :nil];

    }
    
    
    //获取网络的主页信息
    [self giveHomeMess];
    
    
    //注册 cell
    [self.tableView registerClass:[HZHomeCell class] forCellReuseIdentifier:@"homecell"];
    
    
    //预估cell 的行高
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    self.tableView.estimatedRowHeight = 200;
    
}


#pragma mark -
#pragma mark    获取网络的主页信息
-(void)giveHomeMess{
    
    HZHomeVM* homeVM = [[HZHomeVM alloc]init];
    
    
    [homeVM giveHomeMess:^(NSArray *array) {
        
        
        self.array = array;
        
    }];
    
}






#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
 
    return self.array.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    HZHomeCell *cell = [tableView dequeueReusableCellWithIdentifier:@"homecell" forIndexPath:indexPath];
    
  
    //取出数据
    HZHomeCellVM* cellVM = self.array[indexPath.row];
    
    //赋值
    cell.cellVM = cellVM;
//    cell.textLabel.text = model.text;
    
    return cell;
}


/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
